var adminerModule = angular.module('adminerModule', []); // define the module

adminerModule.config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/subscribers', {templateUrl: base_url + 'admin/admindash/subscriber', controller: adminerModule.subscriberList})
    .when('/subscribers/edit/:id', {templateUrl: base_url + 'admin/admindash/subscriberedit', controller: adminerModule.subscriberListEdit})
	  .when('/newsletter/dashboard', {templateUrl: base_url + 'admin/mail/maildashboard', controller: adminerModule.newsletterController})
	  .when('/newsletter/newsletter', {templateUrl: base_url + 'admin/mail/viewnewsletter', controller: adminerModule.newsletterController})
}]);

adminerModule.factory('subscriberList', ['$http', '$rootScope', function($http, $rootScope) {
  var subscribers = [];
  var singleSubscriber = [];

  return {
    getSubscribers: function() {
      return $http.get(base_url + 'admin/adminjson/get_subscriber_json').then(function (response) {
        subscribers = response.data;
        $rootScope.$broadcast('handleSubscribersBroadcast', subscribers);
        return subscribers;
      });
    },
    addSubscriber: function() {

    },
    getSingleSubscriber: function(id) {
      return $http({
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: base_url + 'admin/adminjson/get_single_subscriber_json',
        method: "POST",
        data: $.param({
          'id': id
        })
      })
      .success(function(addData) {
        singleSubscriber = addData;
        $rootScope.$broadcast('singleSubscriberChange', singleSubscriber);
      });
    }
  }
}]);